package com.doform.android.xianggucheng.retrofit;

import com.doform.android.xianggucheng.bean.ALBBBean;
import com.doform.android.xianggucheng.bean.AddressBean;
import com.doform.android.xianggucheng.bean.ApiBean;
import com.doform.android.xianggucheng.bean.ArticleQueryBean;
import com.doform.android.xianggucheng.bean.BillBean1;
import com.doform.android.xianggucheng.bean.CategoryBean;
import com.doform.android.xianggucheng.bean.CategoryQueryBean;
import com.doform.android.xianggucheng.bean.ClientBean;
import com.doform.android.xianggucheng.bean.ClientCategoryBean;
import com.doform.android.xianggucheng.bean.ClientCategoryQueryBean;
import com.doform.android.xianggucheng.bean.ClientDetail;
import com.doform.android.xianggucheng.bean.CommonQueryBean;
import com.doform.android.xianggucheng.bean.GoodBean;
import com.doform.android.xianggucheng.bean.Goods;
import com.doform.android.xianggucheng.bean.GuidBean;
import com.doform.android.xianggucheng.bean.HangQingBean;
import com.doform.android.xianggucheng.bean.HomePager;
import com.doform.android.xianggucheng.bean.ImageBean;
import com.doform.android.xianggucheng.bean.InLibraryDetailBean;
import com.doform.android.xianggucheng.bean.InLibraryDetailBean2;
import com.doform.android.xianggucheng.bean.LibraryBean2;
import com.doform.android.xianggucheng.bean.LoginBean;
import com.doform.android.xianggucheng.bean.MoneyBean2;
import com.doform.android.xianggucheng.bean.OrderBean;
import com.doform.android.xianggucheng.bean.OrderQueryBean;
import com.doform.android.xianggucheng.bean.OrderQueryBean2;
import com.doform.android.xianggucheng.bean.QueryLibraryBean;
import com.doform.android.xianggucheng.bean.RegisterBean;
import com.doform.android.xianggucheng.bean.ReportBean;
import com.doform.android.xianggucheng.bean.ReqSHYSBean;
import com.doform.android.xianggucheng.bean.SHYSBean;
import com.doform.android.xianggucheng.bean.SalesBean;
import com.doform.android.xianggucheng.bean.ShopCarBean;
import com.doform.android.xianggucheng.bean.StarBean;
import com.doform.android.xianggucheng.bean.StarQueryBean;
import com.doform.android.xianggucheng.bean.TiXianBean2;
import com.doform.android.xianggucheng.bean.UnitBean;
import com.doform.android.xianggucheng.bean.VendorBean;
import com.doform.android.xianggucheng.bean.VendorDetailBean;
import com.doform.android.xianggucheng.bean.VersionUpdate;
import com.doform.android.xianggucheng.bean.WorkerBean;
import com.doform.android.xianggucheng.bean.YHKBean;
import com.doform.android.xianggucheng.bean.request_bean.ReEarning;
import com.doform.android.xianggucheng.bean.request_bean.ReqAddAddress;
import com.doform.android.xianggucheng.bean.request_bean.ReqAddArtcle;
import com.doform.android.xianggucheng.bean.request_bean.ReqAddCategoryBean;
import com.doform.android.xianggucheng.bean.request_bean.ReqAddClientBean;
import com.doform.android.xianggucheng.bean.request_bean.ReqAddClientCategoryBean;
import com.doform.android.xianggucheng.bean.request_bean.ReqAddInLibrary;
import com.doform.android.xianggucheng.bean.request_bean.ReqAddInlibraryDetail;
import com.doform.android.xianggucheng.bean.request_bean.ReqAddMeasureUnitBean;
import com.doform.android.xianggucheng.bean.request_bean.ReqAddShopCarBean;
import com.doform.android.xianggucheng.bean.request_bean.ReqAddStarBean;
import com.doform.android.xianggucheng.bean.request_bean.ReqAddVendorBean;
import com.doform.android.xianggucheng.bean.request_bean.ReqBank;
import com.doform.android.xianggucheng.bean.request_bean.ReqBank2;
import com.doform.android.xianggucheng.bean.request_bean.ReqBill;
import com.doform.android.xianggucheng.bean.request_bean.ReqDeleteAddress;
import com.doform.android.xianggucheng.bean.request_bean.ReqDeleteArticleBean;
import com.doform.android.xianggucheng.bean.request_bean.ReqDeleteCategoryBean;
import com.doform.android.xianggucheng.bean.request_bean.ReqDeleteClientBean;
import com.doform.android.xianggucheng.bean.request_bean.ReqDeleteClientCategoryBean;
import com.doform.android.xianggucheng.bean.request_bean.ReqDeleteInLibraryBean;
import com.doform.android.xianggucheng.bean.request_bean.ReqDeleteInLibraryDetailBean;
import com.doform.android.xianggucheng.bean.request_bean.ReqDeleteMeasureUnitBean;
import com.doform.android.xianggucheng.bean.request_bean.ReqDeleteOrder;
import com.doform.android.xianggucheng.bean.request_bean.ReqDeleteShopCarBean;
import com.doform.android.xianggucheng.bean.request_bean.ReqDeleteStarBean;
import com.doform.android.xianggucheng.bean.request_bean.ReqDeleteVendorBean;
import com.doform.android.xianggucheng.bean.request_bean.ReqErCode;
import com.doform.android.xianggucheng.bean.request_bean.ReqHang;
import com.doform.android.xianggucheng.bean.request_bean.ReqHead;
import com.doform.android.xianggucheng.bean.request_bean.ReqHome;
import com.doform.android.xianggucheng.bean.request_bean.ReqImage;
import com.doform.android.xianggucheng.bean.request_bean.ReqListAddress;
import com.doform.android.xianggucheng.bean.request_bean.ReqListArticleBean;
import com.doform.android.xianggucheng.bean.request_bean.ReqListArticleBean2;
import com.doform.android.xianggucheng.bean.request_bean.ReqListCategoryBean;
import com.doform.android.xianggucheng.bean.request_bean.ReqListClientBean;
import com.doform.android.xianggucheng.bean.request_bean.ReqListClientCategoryBean;
import com.doform.android.xianggucheng.bean.request_bean.ReqListInLibraryBean;
import com.doform.android.xianggucheng.bean.request_bean.ReqListInLibraryBean2;
import com.doform.android.xianggucheng.bean.request_bean.ReqListLibraryBean;
import com.doform.android.xianggucheng.bean.request_bean.ReqListMeasureUnitBean;
import com.doform.android.xianggucheng.bean.request_bean.ReqListOrder;
import com.doform.android.xianggucheng.bean.request_bean.ReqListShopCar;
import com.doform.android.xianggucheng.bean.request_bean.ReqListStarBean;
import com.doform.android.xianggucheng.bean.request_bean.ReqListVendorBean;
import com.doform.android.xianggucheng.bean.request_bean.ReqListWorker;
import com.doform.android.xianggucheng.bean.request_bean.ReqLoginBean;
import com.doform.android.xianggucheng.bean.request_bean.ReqMoney;
import com.doform.android.xianggucheng.bean.request_bean.ReqQueryArticle;
import com.doform.android.xianggucheng.bean.request_bean.ReqQueryArticlePrice;
import com.doform.android.xianggucheng.bean.request_bean.ReqQueryCategory;
import com.doform.android.xianggucheng.bean.request_bean.ReqQueryClient;
import com.doform.android.xianggucheng.bean.request_bean.ReqQueryClientCategory;
import com.doform.android.xianggucheng.bean.request_bean.ReqQueryLibraryBean;
import com.doform.android.xianggucheng.bean.request_bean.ReqQueryOrder;
import com.doform.android.xianggucheng.bean.request_bean.ReqQueryOrder2;
import com.doform.android.xianggucheng.bean.request_bean.ReqQueryStar;
import com.doform.android.xianggucheng.bean.request_bean.ReqQueryVendorBean;
import com.doform.android.xianggucheng.bean.request_bean.ReqQueryVendorBean2;
import com.doform.android.xianggucheng.bean.request_bean.ReqRegister;
import com.doform.android.xianggucheng.bean.request_bean.ReqRegister2;
import com.doform.android.xianggucheng.bean.request_bean.ReqRegister3;
import com.doform.android.xianggucheng.bean.request_bean.ReqReport;
import com.doform.android.xianggucheng.bean.request_bean.ReqRetrieve;
import com.doform.android.xianggucheng.bean.request_bean.ReqSale;
import com.doform.android.xianggucheng.bean.request_bean.ReqSubmitInLibrary;
import com.doform.android.xianggucheng.bean.request_bean.ReqSubmitShopCar;
import com.doform.android.xianggucheng.bean.request_bean.ReqUpdateAddress;
import com.doform.android.xianggucheng.bean.request_bean.ReqUpdateArticle;
import com.doform.android.xianggucheng.bean.request_bean.ReqUpdateCategoryBean;
import com.doform.android.xianggucheng.bean.request_bean.ReqUpdateClientBean;
import com.doform.android.xianggucheng.bean.request_bean.ReqUpdateClientCategoryBean;
import com.doform.android.xianggucheng.bean.request_bean.ReqUpdateInLibrary;
import com.doform.android.xianggucheng.bean.request_bean.ReqUpdateMeasureUnitBean;
import com.doform.android.xianggucheng.bean.request_bean.ReqUpdateOrder;
import com.doform.android.xianggucheng.bean.request_bean.ReqUpdatePassword;
import com.doform.android.xianggucheng.bean.request_bean.ReqUpdateShopCar;
import com.doform.android.xianggucheng.bean.request_bean.ReqUpdateStarBean;
import com.doform.android.xianggucheng.bean.request_bean.ReqUpdateVendor;
import com.doform.android.xianggucheng.bean.request_bean.ReqVersion;
import com.doform.android.xianggucheng.bean.request_bean.ReqYHKBean;
import com.doform.android.xianggucheng.help.AppHelp;
import com.doform.android.xianggucheng.utils.Constans;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

import cn.f_ms.network.okhttp.intercepter.globalrequestparams.GlobalHttpParamsIntercepter;
import cn.f_ms.network.okhttp.intercepter.globalrequestparams.HttpParams;
import cn.f_ms.network.okhttp.intercepter.globalrequestparams.UrlQuerys;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.ObservableSource;
import io.reactivex.ObservableTransformer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * 作者 Y_MS
 * Created by ${APEN} on 2017-07-19.
 * GitHub：https://github.com/cxydxpx
 */

public class HttpManager {

    private final IApi mApi;

    public void postUpdateArticlePrice() {

    }

    /**
     * 登录接口
     *
     * @param reqLoginBean
     * @return
     */
    public Observable<LoginBean> login(ReqLoginBean reqLoginBean) {
        return mApi.login2(new ReqHead("system.user.login"), reqLoginBean)
                .compose(new ApiSpecTransformer2<LoginBean>())
                .compose(new SaveUserDataToSp());
    }

    /**
     * 商品列表
     *
     * @param reqListArticleBean
     */
    public Observable<Goods> getArticle2(ReqListArticleBean reqListArticleBean) {
        return mApi.getArticle2(new ReqHead("base.material.list"), reqListArticleBean)
                .compose(new ApiSpecTransformer2<Goods>());
    }

    /**
     * 删除商品
     *
     * @param id
     * @param guid
     * @return
     */
    public Observable<Integer> deleteArticle2(String id, String guid) {
        return mApi.deleteArticle(new ReqHead("base.material.delete"), new ReqDeleteArticleBean(id, guid))
                .compose(new ApiSpecTransformer2<Integer>());
    }

    public Observable<CategoryBean> getCategory2(String s) {
        return mApi.getCategory2(new ReqHead("base.clazz.list"), new ReqListCategoryBean(s))
                .compose(new ApiSpecTransformer2<CategoryBean>());
    }

    /**
     * 获取首页数据
     *
     * @param reqHome
     * @return
     */
    public Observable<HomePager> getHomePagerData(ReqHome reqHome) {
        return mApi.getHomePagerData(new ReqHead("index.list"), reqHome)
                .compose(new ApiSpecTransformer2<HomePager>());
    }

    /**
     * 获取星级数据
     *
     * @param reqListStarBean
     * @return
     */
    public Observable<StarBean> getStar2(ReqListStarBean reqListStarBean) {
        return mApi.getStar(new ReqHead("base.star.list"), reqListStarBean)
                .compose(new ApiSpecTransformer2<StarBean>());
    }

    /**
     * 删除星级数据
     *
     * @param id
     * @param guid
     * @return
     */
    public Observable<Integer> deleteStar(String id, String guid) {
        return mApi.deleteStar(new ReqHead("base.star.delete"), new ReqDeleteStarBean(id, guid))
                .compose(new ApiSpecTransformer2<Integer>());
    }

    /**
     * 添加星级数据
     *
     * @param mName
     * @param mSort
     * @return
     */
    public Observable<GuidBean> addStar(String mName, String mSort) {
        return mApi.addStar(new ReqHead("base.star.action"), new ReqAddStarBean(mName, mSort))
                .compose(new ApiSpecTransformer2<GuidBean>());
    }

    /**
     * 更新客户星级
     *
     * @param id
     * @param mName
     * @param mSort
     * @return
     */
    public Observable<Integer> updateStar(String id, String mName, String mSort) {
        return mApi.updateStar(new ReqHead("base.star.action"), new ReqUpdateStarBean(id, mName, mSort))
                .compose(new ApiSpecTransformer2<Integer>());
    }

    /**
     * 获取客户分类数据
     *
     * @return
     */
    public Observable<ClientCategoryBean> getClientCategory2(String pageId) {
        return mApi.getClientCategory2(new ReqHead("unit.bizclass.list"), new ReqListClientCategoryBean(pageId))
                .compose(new ApiSpecTransformer2<ClientCategoryBean>());
    }

    /**
     * 删除客户分类数据
     *
     * @param id
     * @param guid
     * @return
     */
    public Observable<Integer> deleteClientCategory2(String id, String guid) {
        return mApi.deleteClientCategory2(new ReqHead("unit.bizclass.delete"), new ReqDeleteClientCategoryBean(id, guid))
                .compose(new ApiSpecTransformer2<Integer>());
    }

    /**
     * 添加客户分类数据
     *
     * @param mName
     * @param mSort
     */
    public Observable<GuidBean> addClientCategory2(String mName, String mSort) {
        return mApi.addClientCategory(new ReqHead("unit.bizclass.action"), new ReqAddClientCategoryBean(mName, mSort))
                .compose(new ApiSpecTransformer2<GuidBean>());
    }

    /**
     * 修改客户分类数据
     *
     * @param id
     * @param mName
     * @param mSort
     */
    public Observable<String> updateClientCategory(String id, String guid, String mName, String mSort) {
        return mApi.updateClientCategory(new ReqHead("unit.bizclass.action"), new ReqUpdateClientCategoryBean(id, guid, mName, mSort))
                .compose(new ApiSpecTransformer2<String>());
    }

    /**
     * 获取客户管理数据
     *
     * @param reqListClientBean
     */
    public Observable<ClientBean> getClient2(ReqListClientBean reqListClientBean) {
        return mApi.getClient(new ReqHead("unit.business.list"), reqListClientBean)
                .compose(new ApiSpecTransformer2<ClientBean>());
    }

    /**
     * 删除客户
     *
     * @param id
     * @param guid
     */
    public Observable<Integer> deleteClient(String id, String guid) {
        return mApi.deleteClient(new ReqHead("unit.business.delete"), new ReqDeleteClientBean(id, guid))
                .compose(new ApiSpecTransformer2<Integer>());
    }

    /**
     * 添加客户
     *
     * @param reqAddClientBean
     * @return
     */
    public Observable<GuidBean> addClient2(ReqAddClientBean reqAddClientBean) {
        return mApi.addClient2(new ReqHead("unit.business.action"), reqAddClientBean)
                .compose(new ApiSpecTransformer2<GuidBean>());

    }

    /**
     * 更新客户
     *
     * @param mName
     * @param mPerson
     * @param mPhone
     * @param mDetailAddress
     * @param mCategoryId
     * @param mCategoryName
     * @param mStarId
     * @return
     */
    public Observable<String> updateClient(String mName, String mPerson, String mPhone, String mDetailAddress,
                                           String mCategoryId, String mCategoryName, String mStarId, String id, String guid) {
        return mApi.updateClient(new ReqHead("unit.business.action"), new ReqUpdateClientBean(mName,
                mPerson, mPhone, mDetailAddress, mCategoryId, mCategoryName, mStarId, id, guid))
                .compose(new ApiSpecTransformer2<String>());
    }

    /**
     * 获取客户详情
     *
     * @param id
     * @param guid
     * @return
     */
    public Observable<ArrayList<ClientDetail>> getClientDetail(String id, String guid) {
        return mApi.getClientDetail(new ReqHead("unit.business.query"), new ReqQueryClient(id, guid))
                .compose(new ApiSpecTransformer2<ArrayList<ClientDetail>>());
    }

    /**
     * 获取供应商数据
     *
     * @param reqListVendorBean
     * @return
     */
    public Observable<VendorBean> getVendor2(ReqListVendorBean reqListVendorBean) {
        return mApi.getVendor2(new ReqHead("unit.supplier.list"), reqListVendorBean)
                .compose(new ApiSpecTransformer2<VendorBean>());
    }

    /**
     * 删除供应商
     *
     * @param id
     * @param guid
     * @return
     */
    public Observable<Integer> deleteVendor2(String id, String guid) {
        return mApi.deleteVendor2(new ReqHead("unit.supplier.delete"), new ReqDeleteVendorBean(id, guid))
                .compose(new ApiSpecTransformer2<Integer>());
    }

    /**
     * 添加供应商
     *
     * @param reqAddVendorBean
     * @return
     */
    public Observable<GuidBean> addVendor2(ReqAddVendorBean reqAddVendorBean) {
        return mApi.addVendor2(new ReqHead("unit.supplier.action"), reqAddVendorBean)
                .compose(new ApiSpecTransformer2<GuidBean>());

    }

    /**
     * 获取供应商详情  就是这个接口
     *
     * @param reqQueryVendorBean
     */
    public Observable<ArrayList<VendorDetailBean>> getVendorDetail(ReqQueryVendorBean reqQueryVendorBean) {
        return mApi.getVendorDetail(new ReqHead("unit.supplier.query"), reqQueryVendorBean)
                .compose(new ApiSpecTransformer2<ArrayList<VendorDetailBean>>());
    }

    /**
     * 修改供应商
     *
     * @param reqUpdateVendor
     * @return
     */
    public Observable<String> updateVendor(ReqUpdateVendor reqUpdateVendor) {
        return mApi.updateVendor(new ReqHead("unit.supplier.action"), reqUpdateVendor)
                .compose(new ApiSpecTransformer2<String>());

    }

    /**
     * 修改分类
     *
     * @param name
     * @param sort
     * @param id
     * @param fid
     * @return
     */
    public Observable<String> updateCategory2(String name, String sort, String id, String fid) {
        return mApi.updateCategory2(new ReqHead("base.clazz.action"), new ReqUpdateCategoryBean(name, sort, id, fid))
                .compose(new ApiSpecTransformer2<String>());
    }

    /**
     * 添加分类
     *
     * @param name
     * @param sort
     * @param fid
     */
    public Observable<GuidBean> addCategory2(String name, String sort, String fid) {
        return mApi.addCategory2(new ReqHead("base.clazz.action"), new ReqAddCategoryBean(name, sort, fid))
                .compose(new ApiSpecTransformer2<GuidBean>());
    }

    /**
     * 删除分类
     *
     * @param
     * @param id
     * @param guid
     */
    public Observable<Integer> deleteCategory2(String id, String guid) {
        return mApi.deleteCategory2(new ReqHead("base.clazz.delete"), new ReqDeleteCategoryBean(id, guid))
                .compose(new ApiSpecTransformer2<Integer>());
    }


    /**
     * 获取仓库
     *
     * @param reqListLibraryBean
     * @return
     */
    public Observable<ArrayList<QueryLibraryBean>> getLibraryQuery(ReqListLibraryBean reqListLibraryBean) {
        return mApi.getLibraryQuery(new ReqHead("warehouse.warehouse.query"), reqListLibraryBean)
                .compose(new ApiSpecTransformer2<ArrayList<QueryLibraryBean>>());
    }

    /**
     * 新增采购单
     *
     * @param reqAddInLibrary
     * @return
     */
    public Observable<GuidBean> addInLibrary(ReqAddInLibrary reqAddInLibrary) {
        return mApi.addInLibrary(new ReqHead("warehouse.buyorder.action"), reqAddInLibrary)
                .compose(new ApiSpecTransformer2<GuidBean>());
    }

    /**
     * 添加采购单明细
     *
     * @param reqAddInlibraryDetail
     */
    public Observable<GuidBean> addLibraryDetail(ReqAddInlibraryDetail reqAddInlibraryDetail) {
        return mApi.addLibraryDetail(new ReqHead("warehouse.buydetail.action"), reqAddInlibraryDetail)
                .compose(new ApiSpecTransformer2<GuidBean>());
    }

    /**
     * 提交采购单
     *
     * @param reqSubmitInLibrary
     */
    public Observable<Integer> submitInLibrary(ReqSubmitInLibrary reqSubmitInLibrary) {
        return mApi.submitInLibrary(new ReqHead("warehouse.buyorder.action"), reqSubmitInLibrary)
                .compose(new ApiSpecTransformer2<Integer>());
    }

    /**
     * 获取采购单明细列表
     *
     * @param reqListInLibraryBean2
     */
    public Observable<InLibraryDetailBean2> getInLibraryListDetail(ReqListInLibraryBean2 reqListInLibraryBean2) {
        return mApi.getInLibraryListDetail(new ReqHead("warehouse.buydetail.list"), reqListInLibraryBean2)
                .compose(new ApiSpecTransformer2<InLibraryDetailBean2>());
    }


    /**
     * 仓库商品列表
     *
     * @param reqListInLibraryBean
     */
    public Observable<LibraryBean2> getInLibraryList(ReqListInLibraryBean reqListInLibraryBean) {
        return mApi.getLibraryList(new ReqHead("warehouse.buyorder.list"), reqListInLibraryBean)
                .compose(new ApiSpecTransformer2<LibraryBean2>());

    }

    /**
     * 删除采购单
     *
     * @param reqDeleteInLibraryBean
     */
    public Observable<Integer> deleteInLibrary(ReqDeleteInLibraryBean reqDeleteInLibraryBean) {
        return mApi.deleteInLibrary(new ReqHead("warehouse.buyorder.delete"), reqDeleteInLibraryBean)
                .compose(new ApiSpecTransformer2<Integer>());
    }

    /**
     * 获取采购单详情
     *
     * @param reqQueryLibraryBean
     */
    public Observable<ArrayList<InLibraryDetailBean>> getInLibraryDetail(ReqQueryLibraryBean reqQueryLibraryBean) {
        return mApi.getLibraryDetail(new ReqHead("warehouse.buyorder.query"), reqQueryLibraryBean)
                .compose(new ApiSpecTransformer2<ArrayList<InLibraryDetailBean>>());
    }

    /**
     * 删除采购单明细
     *
     * @param reqDeleteInLibraryDetailBean
     */
    public Observable<Integer> deleteInLibraryDetail2(ReqDeleteInLibraryDetailBean reqDeleteInLibraryDetailBean) {
        return mApi.deleteInLibraryDetail2(new ReqHead("warehouse.buydetail.delete"), reqDeleteInLibraryDetailBean)
                .compose(new ApiSpecTransformer2<Integer>());
    }


    public Observable<ArrayList<ImageBean>> postImg(ReqImage reqImage) {
        return mApi.postImg(new ReqHead("base.material.upload"), reqImage)
                .compose(new ApiSpecTransformer2<ArrayList<ImageBean>>());
    }

    /**
     * 添加商品
     *
     * @param reqAddArtcle
     * @return
     */
    public Observable<GuidBean> addArtcle(ReqAddArtcle reqAddArtcle) {
        return mApi.addArtcle(new ReqHead("base.material.action"), reqAddArtcle)
                .compose(new ApiSpecTransformer2<GuidBean>());
    }

    /**
     * 获取商品详情
     *
     * @param reqQueryArticle
     * @return
     */
    public Observable<ArrayList<GoodBean>> getArticleDetail(ReqQueryArticle reqQueryArticle) {
        return mApi.getArticleDetail(new ReqHead("base.material.query"), reqQueryArticle)
                .compose(new ApiSpecTransformer2<ArrayList<GoodBean>>());
    }

    public Observable<String> updateArticle(ReqUpdateArticle reqUpdateArticle) {
        return mApi.updateArticle(
                new ReqHead("base.material.action"), reqUpdateArticle)
                .compose(new ApiSpecTransformer2<String>());
    }

    /**
     * 余利宝获取收益接口
     *
     * @param reEarning
     * @return
     */
    public Observable<MoneyBean2> getEarnings(ReEarning reEarning) {
        return mApi.getEarning(new ReqHead("yulibao.action"), reEarning)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }

    /**
     * 修改 采购单
     *
     * @param reqUpdateInLibrary
     */
    public Observable<Integer> updateInLibrary(ReqUpdateInLibrary reqUpdateInLibrary) {
        return mApi.updateInLibrary(new ReqHead("warehouse.buyorder.action"), reqUpdateInLibrary)
                .compose(new ApiSpecTransformer2<Integer>());
    }

    /**
     * 发送验证码
     *
     * @param reqRegister
     */
    public Observable<String> postCodeToService(ReqRegister reqRegister) {
        return mApi.postCodeToService(new ReqHead("system.reg.action"), reqRegister)
                .compose(new ApiSpecTransformer2<String>());
    }

    /**
     * 校验验证码
     *
     * @param reqRegister2
     */
    public Observable<Integer> postCodeToService2(ReqRegister2 reqRegister2) {
        return mApi.postCodeToService2(new ReqHead("system.reg.action"), reqRegister2)
                .compose(new ApiSpecTransformer2<Integer>());
    }

    /**
     * 注册
     *
     * @param reqRegister3
     */
    public Observable<RegisterBean> register(ReqRegister3 reqRegister3) {
        return mApi.register(new ReqHead("system.reg.action"), reqRegister3)
                .compose(new ApiSpecTransformer2<RegisterBean>());
    }

    /**
     * 获取账单数据
     *
     * @param reqBill
     */
    public Observable<BillBean1> getBIllData(ReqBill reqBill) {
        return mApi.getBillData(new ReqHead("yulibao.action"), reqBill)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }

    /**
     * 计量单位
     *
     * @param bean
     */
    public Observable<UnitBean> getMeasureUnit(ReqListMeasureUnitBean bean) {
        return mApi.getMeasureUnitData(new ReqHead("base.unit.list"), bean)
                .compose(new ApiSpecTransformer2<UnitBean>());

    }

    /**
     * 添加计量单位
     *
     * @param reqAddMeasureUnitBean
     * @return
     */
    public Observable<GuidBean> addMeasureUnit(ReqAddMeasureUnitBean reqAddMeasureUnitBean) {
        return mApi.addMeasureUnit(new ReqHead("base.unit.action"), reqAddMeasureUnitBean)
                .compose(new ApiSpecTransformer2<GuidBean>());
    }

    /**
     * 删除计量单位
     *
     * @param reqDeleteMeasureUnitBean
     * @return
     */
    public Observable<Integer> deleteMeasureUnit(ReqDeleteMeasureUnitBean reqDeleteMeasureUnitBean) {
        return mApi.deleteMesureUnit(new ReqHead("base.unit.delete"), reqDeleteMeasureUnitBean)
                .compose(new ApiSpecTransformer2<Integer>());
    }

    /**
     * 更新计量单位
     *
     * @param reqUpdateMeasureUnitBean
     * @return
     */
    public Observable<Integer> updateMeasureUnit(ReqUpdateMeasureUnitBean reqUpdateMeasureUnitBean) {
        return mApi.updateMeasureUnit(new ReqHead("base.unit.action"), reqUpdateMeasureUnitBean)
                .compose(new ApiSpecTransformer2<Integer>());
    }

    /**
     * 获取星级下拉数据
     *
     * @return
     */
    public Observable<ArrayList<StarQueryBean>> getStarQuery2() {
        return mApi.getStarQuery2(new ReqHead("base.star.query"), new ReqQueryStar())
                .compose(new ApiSpecTransformer2<ArrayList<StarQueryBean>>());
    }

    /**
     * 获取客户分类下拉数据
     *
     * @param reqQueryClientCategory
     * @return
     */
    public Observable<ArrayList<ClientCategoryQueryBean>> getClientCategoryQuery2(ReqQueryClientCategory reqQueryClientCategory) {
        return mApi.getClientCategoryQuery2(new ReqHead("unit.bizclass.query"), reqQueryClientCategory)
                .compose(new ApiSpecTransformer2<ArrayList<ClientCategoryQueryBean>>());
    }

    /**
     * 查询商品分类
     *
     * @param reqQueryCategory
     * @return
     */
    public Observable<ArrayList<CategoryQueryBean>> getCategoryQuery2(ReqQueryCategory reqQueryCategory) {
        return mApi.getCategoryQuery2(new ReqHead("base.clazz.tree"), reqQueryCategory)
                .compose(new ApiSpecTransformer2<ArrayList<CategoryQueryBean>>())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }

    /**
     * 添加购物车
     *
     * @param id
     * @param guid
     * @param costprice
     */
    public Observable<GuidBean> addShopCar(String id, String guid, String costprice) {
        return mApi.addShopCar(new ReqHead("admin.transaction.car.action"), new ReqAddShopCarBean(id, guid, costprice))
                .compose(new ApiSpecTransformer2<GuidBean>());
    }

    /**
     * 获取购物车列表
     *
     * @param s
     */
    public Observable<ShopCarBean> getShopCarData(String s) {
        return mApi.getShopCar(new ReqHead("admin.transaction.car.list"), new ReqListShopCar(s))
                .compose(new ApiSpecTransformer2<ShopCarBean>());
    }

    /**
     * 获取供应商详细信息
     *
     * @param reqQueryVendorBean2
     * @return
     */
    public Observable<ArrayList<CommonQueryBean>> getVendorQuery2(ReqQueryVendorBean2 reqQueryVendorBean2) {
        return mApi.getVendorQuery2(new ReqHead("unit.supplier.query"), reqQueryVendorBean2)
                .compose(new ApiSpecTransformer2<ArrayList<CommonQueryBean>>());
    }

    /**
     * 更新购物车
     *
     * @param id
     * @param guid
     * @param num
     * @param price
     * @return
     */
    public Observable<GuidBean> updateShopCar(String id, String guid, String num, String price) {
        return mApi.updateShopCar(new ReqHead("admin.transaction.car.action"), new ReqUpdateShopCar(id, guid, num, price))
                .compose(new ApiSpecTransformer2<GuidBean>());
    }

    /**
     * 删除购物车
     *
     * @param id
     * @param guid
     * @return
     */
    public Observable<String> deleteShopCar(String id, String guid) {
        return mApi.deleteShopCar(new ReqHead("admin.transaction.car.delete"), new ReqDeleteShopCarBean(id, guid))
                .compose(new ApiSpecTransformer2<String>());
    }

    /**
     * 获取地址
     *
     * @param reqListAddress
     * @return
     */
    public Observable<ArrayList<AddressBean>> getAddress(ReqListAddress reqListAddress) {
        return mApi.getAddress(new ReqHead("unit.businessaddr.list"), reqListAddress)
                .compose(new ApiSpecTransformer2<ArrayList<AddressBean>>());
    }

    /**
     * 删除地址
     *
     * @param id
     * @param guid
     * @return
     */
    public Observable<Integer> deleteAddress(String id, String guid) {
        return mApi.deleteAddress(new ReqHead("unit.businessaddr.delete"), new ReqDeleteAddress(id, guid))
                .compose(new ApiSpecTransformer2<Integer>());
    }

    /**
     * 获取商品列表 通过供应商ID
     *
     * @param reqListArticleBean2
     * @return
     */
    public Observable<ArrayList<ArticleQueryBean>> getArtiQueryByVendorId(ReqListArticleBean2 reqListArticleBean2) {
        return mApi.getArtiQueryByVendorId(new ReqHead("base.material.query"), reqListArticleBean2)
                .compose(new ApiSpecTransformer2<ArrayList<ArticleQueryBean>>());
    }

    /**
     * 获取订单
     *
     * @param reqListOrder
     * @return
     */
    public Observable<OrderBean> getOrder(ReqListOrder reqListOrder) {
        return mApi.getOrder(new ReqHead("admin.transaction.order.list"), reqListOrder)
                .compose(new ApiSpecTransformer2<OrderBean>());
    }

    /**
     * 提交购物车 生成订单
     *
     * @param clientId
     * @param addrId
     * @param mStrSumPrice
     * @return
     */
    public Observable<GuidBean> submitShopCar(String clientId, String addrId, String mStrSumPrice) {
        return mApi.submitShopCar(new ReqHead("admin.transaction.car.submit"),
                new ReqSubmitShopCar(clientId, addrId, mStrSumPrice))
                .compose(new ApiSpecTransformer2<GuidBean>());
    }

    /**
     * 添加客户地址
     *
     * @param id
     * @param guid
     * @param mName
     * @param mAddress
     * @param mPhone
     * @return
     */
    public Observable<AddressBean> addAddress(String id, String guid, String mName, String mAddress, String mPhone) {
        return mApi.addAddress(new ReqHead("unit.businessaddr.action"),
                new ReqAddAddress(id, guid, mName, mAddress, mPhone))
                .compose(new ApiSpecTransformer2<AddressBean>());
    }

    /**
     * 修改客户地址
     *
     * @param id
     * @param guid
     * @param mName
     * @param mAddress
     * @param mPhone
     */
    public Observable<AddressBean> updateAddress(String id, String guid, String mName, String mAddress, String mPhone) {
        return mApi.updateAddress(new ReqHead("unit.businessaddr.action"),
                new ReqUpdateAddress(id, guid, mName, mAddress, mPhone))
                .compose(new ApiSpecTransformer2<AddressBean>());
    }

    public Observable<ArrayList<WorkerBean>> getWorker() {
        return mApi.getWorker(new ReqHead("emp.emp.query"), new ReqListWorker())
                .compose(new ApiSpecTransformer2<ArrayList<WorkerBean>>());
    }

    /**
     * 修改订单
     *
     * @param reqUpdateOrder
     * @return
     */
    public Observable<Integer> updateOrder(ReqUpdateOrder reqUpdateOrder) {
        return mApi.updateOrder(new ReqHead("admin.transaction.order.action"), reqUpdateOrder)
                .compose(new ApiSpecTransformer2<Integer>());
    }

    /**
     * 获取订单详情
     *
     * @param reqQueryOrder
     * @return
     */
    public Observable<ArrayList<OrderQueryBean>> getOrderDetail(ReqQueryOrder reqQueryOrder) {
        return mApi.getOrderQuery(new ReqHead("admin.transaction.order.query"), reqQueryOrder)
                .compose(new ApiSpecTransformer2<ArrayList<OrderQueryBean>>());
    }


    public Observable<ArrayList<OrderQueryBean2>> getOrderDetail2(ReqQueryOrder2 reqQueryOrder) {
        return mApi.getOrderDetail2(new ReqHead("admin.transaction.orderdetail.query"), reqQueryOrder)
                .compose(new ApiSpecTransformer2<ArrayList<OrderQueryBean2>>());
    }

    /**
     * 获取销售报表
     *
     * @param reqSale
     */
    public Observable<SalesBean> getSale(ReqSale reqSale) {
        return mApi.getSale(new ReqHead("admin.transaction.order.report"), reqSale)
                .compose(new ApiSpecTransformer2<SalesBean>());
    }

    /**
     * 获取报表
     *
     * @param reqReport
     * @return
     */
    public Observable<ReportBean> getReport(ReqReport reqReport) {
        return mApi.getReport(new ReqHead("admin.transaction.order.report"), reqReport)
                .compose(new ApiSpecTransformer2<ReportBean>());
    }

    /**
     * 删除订单
     *
     * @param reqDeleteOrder
     */
    public Observable<Integer> deleteOrder(ReqDeleteOrder reqDeleteOrder) {
        return mApi.deleteOrder(new ReqHead("admin.transaction.order.delete"), reqDeleteOrder)
                .compose(new ApiSpecTransformer2<Integer>());
    }

    /**
     * 获取二维码
     *
     * @param reqErCode
     * @return
     */
    public Observable<String> getErCode(ReqErCode reqErCode) {
        return mApi.getErCode(new ReqHead("system.qrcode.action"), reqErCode)
                .compose(new ApiSpecTransformer2<String>());
    }

    /**
     * 获取行情
     *
     * @param reqHang
     * @return
     */
    public Observable<HangQingBean> getHangQing(ReqHang reqHang) {
        return mApi.getHangQing(new ReqHead("yulibao.action"), reqHang);
    }

    /**
     * 获取银行卡数据
     *
     * @param reqYHKBean
     * @return
     */
    public Observable<YHKBean> getYHKData(ReqYHKBean reqYHKBean) {
        return mApi.getYHKData(new ReqHead("yulibao.action"), reqYHKBean);
    }

    /**
     * 赎回预算
     *
     * @param reqSHYSBean
     * @return
     */
    public Observable<SHYSBean> getSHYS(ReqSHYSBean reqSHYSBean) {
        return mApi.getSHYS(new ReqHead("yulibao.action"), reqSHYSBean);
    }

    public Observable<TiXianBean2> getMoneyFromCard(ReqMoney reqMoney) {
        return mApi.getMoneyFromCard(new ReqHead("yulibao.action"), reqMoney);
    }

    /**
     * 获取商品零售价
     *
     * @param reqQueryArticlePrice
     */
    public Observable<String> getArticle0Price(ReqQueryArticlePrice reqQueryArticlePrice) {
        return mApi.getArticle0Price(new ReqHead("base.material.delprice"), reqQueryArticlePrice)
                .compose(new ApiSpecTransformer2<String>());
    }

    /**
     * 自动更新
     *
     * @param reqVersion
     */
    public Observable<VersionUpdate> updateVersion(ReqVersion reqVersion) {
        return mApi.updateVersion(new ReqHead("system.update.action"), reqVersion)
                .compose(new ApiSpecTransformer2<VersionUpdate>());
    }

    /**
     * 根据支付宝返回的数据 获取银行卡数据
     *
     * @param reqBank
     * @return
     */
    public Observable<String> getBankData(ReqBank reqBank) {
        return mApi.getBankData(new ReqHead("bankimg.query"), reqBank)
                .compose(new ApiSpecTransformer2<String>());
    }

    /**
     * 找回密码 发送验证码
     *
     * @param reqRetrieve
     */
    public Observable<String> getErCode2(ReqRetrieve reqRetrieve) {

        return mApi.getErCode2(new ReqHead("system.reg.action"),reqRetrieve)
                .compose(new ApiSpecTransformer2<String>());


    }

    public Observable<RegisterBean> updatePassword(ReqUpdatePassword reqUpdatePassword) {
        return mApi.updatePassword(new ReqHead("system.reg.action"),reqUpdatePassword)
                .compose(new ApiSpecTransformer2<RegisterBean>());
    }

    public Observable<ALBBBean> getBankData2(ReqBank2 reqBank2) {
        return mApi.getBankData2(new ReqHead("bankimg.query"),reqBank2);
    }


    /**
     * 余利宝行情查询
     * //     * @param reEarning
     */
//    public Observable<MoneyBean3> getMarket(ReEarning reEarning) {
//        return ;
//    }

    private static class SingleInstance {
        private final static HttpManager INSTANCE = new HttpManager();
    }

    public static HttpManager instance() {
        return SingleInstance.INSTANCE;
    }

    private HttpManager() {

        GlobalHttpParamsIntercepter globleParams2 = new GlobalHttpParamsIntercepter(
                new GlobalHttpParamsIntercepter.OnNeedHttpParams() {
                    @Override
                    public HttpParams getParams(Request request) {
                        return new HttpParams.Builder()
                                // set URL_Query params
                                .setUrlQuerys(
                                        new UrlQuerys.Builder()
                                                .build()
                                )
                                .build();
                    }
                }
        );

        OkHttpClient.Builder okHttpClientBuild = new OkHttpClient.Builder()
                .connectTimeout(60, TimeUnit.SECONDS)
                .writeTimeout(60, TimeUnit.SECONDS)
                .readTimeout(60, TimeUnit.SECONDS)
                .addInterceptor(
                        globleParams2
                );

            /*
            OkHttp Log
             */
        HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
        logging.setLevel(HttpLoggingInterceptor.Level.BODY);
        okHttpClientBuild.addInterceptor(logging);

        Gson gson = new GsonBuilder()
                .registerTypeAdapter(ApiBean.class, new ApiSpecBeanJsonDeserializer())
                .create();

        OkHttpClient okHttpClient = okHttpClientBuild.build();

        mApi = new Retrofit.Builder()
//                .baseUrl(Constans.FORMAL_URL)
                .baseUrl(Constans.TEST_URL)
                .client(okHttpClient)
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create(gson))
                .build()
                .create(IApi.class);

    }


    private class SaveUserDataToSp implements ObservableTransformer<LoginBean, LoginBean> {
        @Override
        public ObservableSource<LoginBean> apply(@NonNull Observable<LoginBean> upstream) {
            return upstream
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .map(new Function<LoginBean, LoginBean>() {
                        @Override
                        public LoginBean apply(@NonNull LoginBean str) throws Exception {
                            if (null != str) {
                                AppHelp.getInstance().setToken(str.getThusertoken());
                            }
                            return str;
                        }
                    });
        }
    }


    private class ApiSpecTransformer2<T> implements ObservableTransformer<ApiBean<T>, T> {
        @Override
        public ObservableSource<T> apply(@NonNull Observable<ApiBean<T>> upstream) {
            return upstream
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .flatMap(new Function<ApiBean<T>, ObservableSource<T>>() {
                        @Override
                        public ObservableSource<T> apply(@NonNull final ApiBean<T> bean) throws Exception {
                            return Observable.create(new ObservableOnSubscribe<T>() {
                                @Override
                                public void subscribe(@NonNull ObservableEmitter<T> e) throws Exception {
                                    if (bean.isRs()) {
                                        e.onNext(bean.getData());
                                        e.onComplete();
                                    } else {
                                        e.onError(new Throwable(bean.getErrorMessage()));
                                    }
                                }
                            });
                        }
                    });
        }
    }

}
